const state = {
  main: 0,
  currentWin: null,
  URL: "",
  My_percent: 0,
  // 播放模块
  currentTime: 0,
  duration: 0,
  play_state: false,
  song: null,
  drawer: false,
  volume: 0.6,
  // 收藏模块
  reload_DB: false,
  playing_list: null,
  playing_list_insert: false,
  play_mode: "0",
  lyric: null,
  // 数据库模块
  currentEmail: ""
}

const getters = {
  My_percent(state) {
    return state.My_percent
  }
}

const mutations = {
  // 数据库模块
  save_currentEmail(state, currentEmail) {
    state.currentEmail = currentEmail;
  },
  // 收藏模块
  need_reload_DB(state, reload_DB) {
    state.reload_DB = reload_DB;
  },
  // 播放模块参数
  remove_playing_list_song(state, song) {
    let index = state.playing_list.findIndex((item) => {
      return item.id === song.id;
    });
    state.playing_list.splice(index, 1);
  },
  save_lyric(state, lyric) {
    state.lyric = lyric;
  },
  Switch_song(state, last_next) {
    let play_mode = state.play_mode;
    let playing_list = state.playing_list;
    let song = state.song;

    let index = playing_list.findIndex((item) => {
      return item.id === song.id;
    });

    switch (last_next) {
      case 3:
        switch (play_mode) {
          case "0":
            if (index === 0) {
              state.song = playing_list[playing_list.length - 1];
            } else {
              state.song = playing_list[index - 1];
            }
            break;
          case "1":
            state.song = playing_list[index];
            state.currentTime = 0;
            break;
          case "2":
            let randomIndex = Math.floor(Math.random() * playing_list.length);
            if (randomIndex === index) {
              if (index === playing_list.length - 1) {
                state.song = playing_list[0];
              } else {
                state.song = playing_list[index + 1];
              }
            } else {
              state.song = playing_list[randomIndex];
            }
            break;
        }
        break;
      case 4:
        switch (play_mode) {
          case "0":
            if (index === playing_list.length - 1) {
              state.song = playing_list[0];
            } else {
              state.song = playing_list[index + 1];
            }
            break;
          case "1":
            state.song = playing_list[index];
            state.currentTime = 0;
            break;
          case "2":
            let randomIndex = Math.floor(Math.random() * playing_list.length);
            if (randomIndex === index) {
              if (index === 0) {
                state.song = playing_list[playing_list.length - 1];
              } else {
                state.song = playing_list[index - 1];
              }
            } else {
              state.song = playing_list[randomIndex];
            }
            break;
        }
        break;

    }
    state.play_state = true;
  },
  upload_play_mode(state, play_mode) {
    state.play_mode = play_mode;
  },
  clear_playing_list(state) {
    state.playing_list = null;
  },
  insert_playing_list(state, song) {
    state.playing_list_insert = true;
    if (!state.playing_list) {
      state.playing_list = [];
    }
    state.playing_list.push(song);
  },
  change_playing_list_insert(state, playing_list_insert) {
    state.playing_list_insert = playing_list_insert;
  },
  upload_playing_list(state, playing_list) {
    state.playing_list = playing_list;
  },
  change_volume(state, volume) {
    state.volume = volume;
  },
  change_play_state(state, play_state) {
    state.play_state = play_state;
  },
  change_song(state, song) {
    state.song = song;
  },
  change_drawer(state, drawer) {
    state.drawer = drawer;
  },
  // 
  DECREMENT_MAIN_COUNTER(state) {
    state.main--
  },
  INCREMENT_MAIN_COUNTER(state) {
    state.main++
  },
  saveCurrentWin(state, win) {
    state.currentWin = win;
  },
  saveHomePage(state, URL) {
    state.URL = URL
  },
  // 保存当前播放时间
  save_currentTime(state, currentTime) {
    state.currentTime = currentTime;
  },
  // 保存总时长
  save_duration(state, duration) {
    state.duration = duration;
  },
  save_My_percent(state, My_percent) {
    state.My_percent = My_percent;
  }

}

const actions = {
  someAsyncTask({ commit }) {
    // do something async
    commit('INCREMENT_MAIN_COUNTER')
  },
}

export default {
  state,
  getters,
  mutations,
  actions
}
